from time import sleep
import string, random
from threading import Thread, active_count

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.chrome.options import Options
from rich import print as rprint

class Browser:
    def __init__(self):
        options = Options()
        options.page_load_strategy = 'eager'
        options.add_argument('--start-maximized')
        options.add_argument('--incognito')
        self.browser = webdriver.Chrome(options=options)

    def input_content(self, id, con='name'):
        content = ''
        for i in random.choices(string.ascii_letters, k=7):
            content += i
        if con == 'email':
            content += '@gmail.com'
        elif con == 'pwd':
            content = 'https://www.vzone.pw/auth/register'
        self.browser.find_element(By.ID, id).send_keys(content)

    def regester(self, url):
        try:
            self.browser.get(url)
            Browser.input_content(self, 'name')
            # 区分两个网站，在邮箱中输入不同的内容
            if 'googoo' in url:
                Browser.input_content(self, 'email', con='email')
            else:
                Browser.input_content(self, 'email', con='name')
            Browser.input_content(self, 'passwd', con='pwd')
            Browser.input_content(self, 'repasswd', con='pwd')
            WebDriverWait(self.browser,20).until(EC.presence_of_element_located((By.CLASS_NAME, 'geetest_radar_tip')))      # 等待出现滑动验证
            self.browser.find_element(By.CLASS_NAME, 'geetest_radar_tip').click()       # 点击进行验证
            WebDriverWait(self.browser,60).until(EC.text_to_be_present_in_element((By.CLASS_NAME, 'geetest_success_radar_tip_content'), '验证成功'))
            self.browser.find_element(By.ID, 'register-confirm').click()        # 点击注册
            WebDriverWait(self.browser,20).until(EC.presence_of_element_located((By.XPATH, '//div[@class="swal2-actions"]/button')))     # 等待弹出注册成功
            self.browser.find_element(By.XPATH, '//div[@class="swal2-actions"]/button').click()     # 点击确定
        except:
            sleep(1)
            self.regester(url)

    def go(self):
        while True:
            try:
                Browser.regester(self, 'https://cdn.googoo.cyou/auth/register')
                WebDriverWait(self.browser,30).until(EC.title_is('首页 — Go加速'))  # 等待首页加载
                break
            except:
                continue
        WebDriverWait(self.browser,30).until(EC.presence_of_element_located((By.XPATH, '//div[@class="modal-content"]//button')))   # 等待弹出重要通知
        sleep(2)
        self.browser.find_element(By.XPATH, '//div[@class="modal-content"]//button').click ()        # 关闭重要通知
        sleep(1.5)
        self.browser.find_element(By.XPATH, '//div[@class="wizard-step wizard-step-active"]').click()          # 前往商店订购免费试用
        self.browser.execute_script('window.scrollTo(0,500)')
        self.browser.find_element(By.XPATH, '//div[@class="row"]/div[1]//div[@class="pricing-cta"]').click()    # 订购试用套餐
        sleep(1.5)
        self.browser.find_element(By.XPATH, '//div[@class="swal2-actions"]/button').click()         # 关闭确认窗口
        WebDriverWait(self.browser,5).until(EC.title_is('首页 — Go加速'))
        try:    # 避免没有通知弹窗的时候报错
            self.browser.find_element(By.XPATH, '//div[@class="modal-content"]//button').click()        # 关闭重要通知
        except:
            pass
        self.browser.find_element(By.ID, 'checkin-div').click()     # 签到
        # 这里总是报错，但是我没找到任何错误
        # self.browser.find_element(By.XPATH, '//div[@class="swal2-actions"]/button[1]').click()   # 关闭签到成功弹窗
        self.browser.execute_script('window.scrollTo(0,300)')
        # global v2rayLink
        v2rayLink = self.browser.find_element(By.XPATH, '//div[@class="buttons"]/a[5]').get_attribute('data-clipboard-text')
        rprint('[green]{}\nGo加速订阅链接：{}\n{}'.format('-'*90, v2rayLink, '-'*90))

    def re(self, url):
        while True:
            try:
                Browser.regester(self, url)
                WebDriverWait(self.browser,30).until(EC.title_is('首页 — 伊云'))
                break
            except:
                continue
        self.browser.execute_script('window.scrollTo(0,500)')
        global v2rayLink
        v2rayLink = self.browser.find_element(By.XPATH, '//div[@class="buttons"]/a[7]').get_attribute('data-clipboard-text')
        rprint('[green]{}\nre机场V2Ray订阅链接：{}\n{}'.format('-'*110, v2rayLink, '-'*110))
        global clashLink
        clashLink = v2rayLink.split('?')[0] + '?clash=1'
        rprint('[green]{}\nre机场clash订阅链接：{}\n{}'.format('-'*110, clashLink, '-'*110))
        self.browser.get('https://www.recear.co/user/invite')
        invite_link = self.browser.find_element(By.XPATH, '//div[@class="mt-4"]/a').get_attribute('data-clipboard-text')
        self.browser.close()
        return invite_link

choice = input('是否需要注册 Go加速（回车=>是，其他=>否）：')

B = Browser()
if choice == '':
    B.go()
    next = input('回车开始注册re：')

url = 'https://www.recear.co/auth/register'     # re
# url = 'https://www.ppyu.top/auth/register'    # 泡泡云
invite_link = B.re(url)
# invite_link = 'https://www.reyao.top/auth/register?code=kYed'

for i in range(4):
    Thread(target=Browser().regester, args=(invite_link,)).start()
    rprint(f'已打开 1 个邀请注册页面，累计打开 {i+1} 个')

while True:
    if active_count() == 1:
        break

rprint('[yellow]邀请完成', )
rprint('邀请链接：', invite_link)
rprint('[green]{}\nre机场V2Ray订阅链接：{}\n{}'.format('-'*110, v2rayLink, '-'*110))
rprint('[green]{}\nre机场clash订阅链接：{}\n{}'.format('-'*110, clashLink, '-'*110))
next = input('回车关闭窗口')



# 网址发布页：https://x-go.gitbook.io/untitled/